package com.big.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.big.app.dao.FoodMapDao;
import com.big.app.dao.FoodsDao;
import com.big.app.dto.FoodsDto;
import com.big.app.entity.FoodMap;
import com.big.app.entity.Foods;
import com.big.app.service.FoodsService;
import com.big.app.util.JedisUtils;
import com.big.app.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FoodsServiceImpl implements FoodsService {
    private Logger logger= LoggerFactory.getLogger(FoodsServiceImpl.class);

    @Autowired
    private FoodsDao dao;
    @Autowired
    private FoodMapDao mapDao;
    @Override
    public R queryListAll() {
        logger.info("美食展示开始。。");

        /*查询全部*/
        List<FoodsDto> foods= dao.selectListAll();
        logger.info("美食查询结束。。");
        return new R(200,foods);

    }

    @Override
    public R queryDetail(int id) {

        FoodsDto foodsDto= dao.selectDetail(id);
        /*浏览量加一*/
        int looknum=foodsDto.getLooknum()+1;
        Foods foods =new Foods(foodsDto.getId(),foodsDto.getName(),foodsDto.getImgurl(),foodsDto.getPrice(),foodsDto.getSite(),foodsDto.getLovenum(),
                looknum,foodsDto.getCtime(),foodsDto.getFid());
        dao.updateById(foods);
        FoodsDto foodsDto1= dao.selectDetail(id);
        /*地址*/
        String name = foodsDto1.getName();
        if (!JedisUtils.esists(name)){
            QueryWrapper<FoodMap> queryWrapper =new QueryWrapper();
            queryWrapper.eq("name",name);
            FoodMap foodMap = mapDao.selectOne(queryWrapper);
            /*存redis*/
            JedisUtils.geoAdd(foodMap.getName(),foodMap.getX(),foodMap.getY(),foodMap.getName());
        }
        return new R(200,foodsDto1);
    }

    @Override
    public R queryListName(String name) {
        if (name != null) {
            name = "%"+name+"%";
        }
        List<FoodsDto> foodsDtos = dao.selectByName(name);
        return new R(200,foodsDtos);
    }

    @Override
    public R queryListType(int flag) {
       List<FoodsDto> foodsDtos= dao.selectByFlag(flag);
        return new R(200,foodsDtos);
    }
}
